# -*- coding: utf-8 -*-
# Copyright (C) 2007 Boris Dušek
# See the file LICENCE in the top-level directory of this distribution
# for licensing information.
# vim:ts=4:sw=4:et

import unittest
from army.utils import endian

class EndiannessTest(unittest.TestCase):

    def __init__(self):
        pass

    def setUp(self):
        self.ends = [endian.LittleEndianness(),endian.BigEndianness()]
        self.byte_order_test_cases = [
            # exponent, array of bytes in memory, array of results for each endianness
            # (result = array of bytes in the order from least to most significant)
            # (exponent = 2**exponent == len(array of bytes))
            [3, '\x11\x22\x33\x44\x55\x66\x77\x88',
                [
                    '\x11\x22\x33\x44\x55\x66\x77\x88',
                    '\x88\x77\x66\x55\x44\x33\x22\x11',
                ]
            ],
            [2, '\x11\x22\x33\x44', ['\x11\x22\x33\x44', '\x44\x33\x22\x11',]],
            [1, '\x11\x22', ['\x11\x22', '\x22\x11',]],
            ]
        self.bits_test_cases = [
#            [0, [171], [[1,1,0,1,0,1,0,1], [1,1,0,1,0,1,0,1]]],
            [1, [9,4095],
                [
                    [1,0,0,1,0,0,0,0, 0,0,0,0,1,0,0,0],
                    [0,0,0,0,1,0,0,0, 1,0,0,1,0,0,0,0]
                ]
            ],
        ]

    def testByteOrder(self):
        for exp,in_order,out_orders in self.byte_order_test_cases:
            for end,out_order in zip(self.ends,out_orders):
                self.assertEqual(out_order,end.byte_order(in_order,exp))

    def testBits(self):
        for exp,bytes,end_bits in self.bits_test_cases:
            for end,bits in zip(self.ends,end_bits):
                self.assertEqual(bits,end.bits(bytes,exp))

def main():
    test = EndiannessTest()
    test.setUp()
    test.testBits()

if __name__ == '__main__':
    #unittest.main()
    main()
